<?php
/**
 * 新巴枪接口
 *
 * $sdkClient = new sdkClient();
 * $res = $sdkClient->plogin('xxx','xxx');
 *
 * @author ljp
 */

namespace soa\sto;

use soa\client;

class pda extends client{

    protected $service_name = 'sto_pda';
    protected $_timeout = 5;

    public $service_url = 'http://pdatest.sto.cn:19090/receive/t/comsd.action';
    public $apiUrl = 'http://pdatest.sto.cn:19090/receive';

    public $opOrgCode = 888880; //操作网点
    public $opTerminal = 'sto_message'; //操作终端
    public $connectionType = 2; //0：WiFi   1：GPRS  2：WLAN


    public function request_params($api,$data)
    {

        $_sys_data = array(
            'tid' => $this->opTerminal,
            'trace' => "",
            'mty' => "0",
            'opOrgCode' => $this->opOrgCode,
            'deviceType' => "PC",
            'opTerminal' => $this->opTerminal,
            'verifyDate' => date('Y-m-d H:i:s'),
            'connectionType' => $this->connectionType,
            'os' => "",
            'osVersion' => "0.0.0.0",
            'softVersion' => "0.0.0.0",
            'pdaType' => ""
        );

        $data = array_merge($_sys_data, $data);logDebug($data, 'sto-gun');
        $request_data = json_encode($data, JSON_UNESCAPED_UNICODE);
        $request_len = '000999';
        return array('url'=> $this->apiUrl.'/'.$api,'reqData' => $request_len . $request_data);
    }

    public function request_api($api){
        return $this->service_url;
    }

    public function parse($response, $code = 'code', $msg = 'msg', $data = 'data'){

        $return_obj = new \stdClass();
        $return_obj->$code = 0;
        $return_obj->error = 0;
        $return_obj->$msg = '';
        $return_obj->$data = array();

        $res = json_decode($response, true);
        if (json_last_error() != JSON_ERROR_NONE || !isset($res['rspData'])) {
            $return_obj->$code = 999;
            $return_obj->$msg = $response;
            $return_obj->error = 1;

            return $return_obj;
        }

        //fix respData format
        $res['rspData'] = stripcslashes(trim($res['rspData'],'"'));

        $res = mb_substr($res['rspData'], 6);
        $res = json_decode($res, true);

        $return_obj->$code = 0;
        $return_obj->$msg = '';
        $return_obj->$data = $res;
        return $return_obj;
    }

    function plogin($empCode, $pdaPassword)
    {
        return $this->get('plogin.action', array('empCode' => $empCode, 'pdaPassword' => $pdaPassword));
    }

    function plogout($opOrgCode, $empCode)
    {

        $params = array(
            'opOrgCode' => $opOrgCode,
            'empCode' => $empCode
        );
        return $this->post('plogout.action', $params);
    }

    function syncTime()
    {
        return $this->get('syncTime.action');
    }

    function hardwareGather()
    {
        return $this->get('hardwareGather.action');
    }

    function syssyn()
    {
        return $this->get('syssyn.action');
    }

    /**
     * @param $session_id
     * @param $downType 员工(downType:EMP)  组织机构(downType:ORG) 行政区域(downType:REGION)
     * @return array|mixed
     */
    function basicdown($session_id, $downType,$version = '1000') {
        return $this->post('basicdown.action',array('sessionId'=>$session_id,'downType'=>$downType,'version'=>$version));
    }

    function systemUpdate(){
        return $this->get('systemUpdate.action');
    }

    function orderdownload($data){
        return $this->get('orderdownload.action', $data);
    }

    function orderpickup($data){
        return $this->get('orderpickup.action', $data);
    }

    function orderreback($data){
        return $this->get('orderreback.action', $data);
    }

    function deliveryAndSignature(){
        return $this->get('deliveryAndSignature.action');
    }

    function deliveryScope(){
        return $this->get('deliveryScope.action');
    }

    function msgreceive(){
        return $this->get('msgreceive/message.action');
    }

    /**
     * 收件、发件、派件、签收、问题件操作
     * @param $opData array 打包的所有数据，包括基础数据
     * @Author wrj
     * @date 2015-08-21
     * @return array|mixed
     */
    function scane3($opData){
        return $this->post('message.action', $opData);
    }

    /**
     * 基础资料下载
     * @param $arr
     * @return bool
     */
    function pda($arr)
    {
        return $this->post('basicdown.action', $arr);
    }
}
